﻿using Core.Infrastructures.Cores.DataStore;
using Core.Infrastructures.Cores.Injections.Dependencies;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;

namespace Core.Repositories
{
    public partial class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : class, IEntity, new()
    {
        protected IDataAccessor _dataAccessor;

        public virtual TEntity GetById<TKey>(TKey id)
            => _dataAccessor.DbContext.Queryable<TEntity>().InSingle(id);

        public virtual int Insert(TEntity entity)
            => _dataAccessor.DbContext.Insertable(entity).ExecuteCommand();

        public int Insert<T>(T entity) where T : class, new()
       => _dataAccessor.DbContext.Insertable(entity).ExecuteCommand();

        public virtual int Update(TEntity entity)
            => _dataAccessor.DbContext.Updateable(entity).ExecuteCommand();

        public int Update<T>(T entity) where T : class, new()
        => _dataAccessor.DbContext.Updateable(entity).ExecuteCommand();

        public virtual int UpdateColumns(TEntity entity, Expression<Func<TEntity, object>> columns)
            => _dataAccessor.DbContext.Updateable(entity).UpdateColumns(columns).ExecuteCommand();
        public int UpdateColumns<T>(T entity, Expression<Func<T, object>> columns) where T : class, new()
          => _dataAccessor.DbContext.Updateable(entity).UpdateColumns(columns).ExecuteCommand();

        public virtual int Delete<TKey>(TKey id)
            => _dataAccessor.DbContext.Deleteable<TEntity>().In(id).ExecuteCommand();

        public virtual bool Any(Expression<Func<TEntity, bool>> expression)
            => _dataAccessor.DbContext.Queryable<TEntity>().Any(expression);

        public virtual IEnumerable<TEntity> GetList(Expression<Func<TEntity, bool>> expression, string orderBy = null)
            => _dataAccessor.DbContext.Queryable<TEntity>().WhereIF(expression != null, expression).OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy).ToList();

        public virtual IEnumerable<TEntity> GetList(string expression, string orderBy = null)
            => _dataAccessor.DbContext.Queryable<TEntity>().WhereIF(!string.IsNullOrEmpty(expression), expression).OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy).ToList();

        public virtual IEnumerable<TEntity> GetListByPage(Expression<Func<TEntity, bool>> expression, string orderBy, int pageIndex, int pageSize, ref int records, ref int totalPage)
            => _dataAccessor.DbContext.Queryable<TEntity>().WhereIF(expression != null, expression).OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy).ToPageList(pageIndex, pageSize, ref records, ref totalPage);

        public virtual IEnumerable<TEntity> GetListByPage(string expression, string orderBy, int pageIndex, int pageSize, ref int records, ref int totalPage)
            => _dataAccessor.DbContext.Queryable<TEntity>().WhereIF(!string.IsNullOrEmpty(expression), expression).OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy).ToPageList(pageIndex, pageSize, ref records, ref totalPage);
    }
}
